<?php
session_start();
$servername = "localhost";
$username = "root"; // 数据库用户名
$password = "store"; // 数据库密码
$dbname = "aiweb"; // 数据库名称

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM user WHERE uname='$username' AND pass='$password'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // 登录成功，获取用户信息
        $userInfo = $result->fetch_assoc();

        // 将用户信息保存到会话中
        $_SESSION['userid'] = $userInfo['uid']; // 存储用户ID
        // 登录成功，跳转至管理页
        header("Location: pages/dashboard.php");
    } else {
        // 登录失败，提示信息并跳转至注册页面
        echo "<script>alert('无效的用户名和密码'); window.location.href='pages/login-page.php';</script>";
    }
}

$conn->close();
?>
